﻿using AntdUI;
using System.Globalization;
namespace DataCollection.WinForm.Forms
{
    public partial class KeyBoardForm : Window
    {
        #region 属性

        #endregion


        #region 系统事件
        static public AntdUI.Input input = null!;

        //防止窗体闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        public KeyBoardForm()
        {
            InitializeComponent();
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void KeyBoardForm_LocationChanged(object sender, EventArgs e)
        {
            CheckAndAdjustFormPosition();
        }

        private void CheckAndAdjustFormPosition()
        {
            Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;

            int newLeft = Math.Min(Math.Max(this.Left, workingArea.Left), workingArea.Right - this.Width);
            int newTop = Math.Min(Math.Max(this.Top, workingArea.Top), workingArea.Bottom - this.Height);

            this.Location = new Point(newLeft, newTop);
        }

        private void KeyBoardForm_Load(object sender, EventArgs e)
        {
            if (input != null)
            {
                ContentInput.PlaceholderText = input.PlaceholderText;
            }
        }

        //这是 0~9 以及 . 按钮绑定的事件
        void Button_Click(object sender, EventArgs e)
        {
            AntdUI.Button B = (AntdUI.Button)sender;
            if (B != null)
            {
                string buttonText = B.Text;
                int selectionStart = ContentInput.SelectionStart; // 保存当前焦点位置
                ContentInput.Text = ContentInput.Text.Insert(selectionStart, buttonText); // 在焦点位置插入按钮文字
                ContentInput.SelectionStart = selectionStart + buttonText.Length; // 设置焦点位置
                ContentInput.Focus(); // 重新设置焦点到文本框
            }
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            if (ContentInput.Text.Length >= 1)
            {
                int selectionStart = ContentInput.SelectionStart;
                ContentInput.Text = ContentInput.Text.Remove(selectionStart - 1, 1);
                ContentInput.SelectionStart = selectionStart - 1;
                ContentInput.Focus();
            }

        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            ContentInput.Text = string.Empty;
            ContentInput.Focus();
        }

        private void ContentInput_VerifyChar(object sender, InputVerifyCharEventArgs e)
        {
            NumberFormatInfo numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
            string decimalSeparator = numberFormatInfo.NumberDecimalSeparator,
                groupSeparator = numberFormatInfo.NumberGroupSeparator, negativeSign = numberFormatInfo.NegativeSign;
            string keyInput = e.Char.ToString();
            if (char.IsDigit(e.Char))
            {
                e.Result = true; // 数字可以
            }
            else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) || keyInput.Equals(negativeSign))
            {
                if (ContentInput.Text.Contains('.'))
                {
                    e.Result = false; // 小数分隔符可以
                }
                else
                {
                    e.Result = true; // 小数分隔符可以
                }
            }
            else if (e.Char == '\b')
            {
                e.Result = true; // Backspace键可以
            }
            else if (e.Char == '。')
            {
                e.ReplaceText = ".";
                e.Result = true; // 中文句号替换为英文句号
            }
            else
            {
                e.Result = false;
            }
        }
        public static int CountCharacter(string str, char character)
        {
            return str.Count(c => c == character);
        }
        private void ContentInput_TextChanged(object sender, EventArgs e)
        {
            if (CountCharacter(ContentInput.Text, '.') > 1)
            {
                int selectionStart = ContentInput.SelectionStart;
                ContentInput.Text = ContentInput.Text.Remove(selectionStart, 1);
                ContentInput.SelectionStart = selectionStart - 1;
                ContentInput.Focus();
            }
        }

        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            string text = ContentInput.Text;
            if (text.Length <= 0)
            {
                text = "0";
            }
            input.Text = text;
            input.SelectionStart = ContentInput.Text.Length;
            input.Focus();
            this.Close();
        }
        #endregion
    }
}
